home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / freeli20.zip / progs / view.asm < prev    next >
Assembly Source File  |  1996-07-01  |  6KB  |  205 lines

  1. ; VIEW v1.0: File viewer.
  2.  
  3. Ideal
  4. Jumps
  5.  
  6. Public      main
  7. Extrn       startup:near
  8.  
  9. Macro       lcall p,a,b,c,d,e,f,g,h ;; library call
  10.  
  11.             ifnb <a>
  12.               push a                ;; if args, push first arg
  13.               lcall p,b,c,d,e,f,g,h ;; and recurse . . .
  14.             else
  15.               extrn p:near          ;; declare procedure
  16.               call p                ;; call procedure
  17.             endif
  18.  
  19. EndM
  20.  
  21. Model Tiny
  22. Codeseg
  23. P186
  24. Org 100h
  25.  
  26. Start:      jmp startup
  27.  
  28. ;****************** Data Section
  29.  
  30. HelpStr     db '  FreeLib VIEW v1.0      Line: 1',10 dup(32)
  31.             db 'Keys: Up Down PgUp PgDn Home End Esc'
  32. JunkBuf     db 12 dup(32),0
  33. OutOfMem    db 'Out of memory',13,10,0
  34. Syntax      db 'Syntax:  VIEW <file>',0
  35. FNotFound   db 'File not found',0
  36. Indexing    db 'Indexing file . . .',0
  37.  
  38. ;****************** 'main' procedure
  39.  
  40. Proc        main
  41.  
  42.             test cx,cx
  43.             jz BadSyntax
  44.  
  45.             lcall fsetbuf 8192      ;Set file buffers to 8K
  46.  
  47.             mov bx,[di]             ;Open the file
  48.             lcall fopen bx,0
  49.             test ax,ax              ;File not found?
  50.             jz NoFile
  51.  
  52.             mov bp,ax               ;BP = handle
  53.  
  54.             lcall allocmem 91*33    ;Allocate the string buffer
  55.             test ax,ax
  56.             jnz Continue
  57.  
  58. NoMem:      lcall fclose bp         ;Close the file
  59.             push offset OutOfMem    ;Display 'Out of memory' message
  60. PutRet:     lcall puts
  61.             ret                     ;Return
  62. BadSyntax:  push offset Syntax      ;Display 'Syntax' message
  63.             jmp PutRet
  64. NoFile:     push offset FNotFound   ;Display 'File not found' message
  65.             jmp PutRet
  66.  
  67. Continue:   mov si,ax               ;SI = string buffer
  68.  
  69.             lcall getmfree          ;Get free memory
  70.             mov cx,ax
  71.             shr cx,2                ;Max lines
  72.             jz NoMem                ;Zero, return
  73.             lcall allocmem ax       ;Allocate the memory
  74.             mov di,ax               ;DI = index buffer
  75.  
  76.             lcall puts offset(Indexing)   ;Display 'Indexing' message
  77.  
  78.             xor bx,bx               ;Line count
  79.             push di                 ;Save DI
  80.  
  81. IndexLoop:  lcall ftell bp          ;Get file pointer
  82.             stosw                   ;Store position
  83.             xchg ax,dx
  84.             stosw
  85.  
  86.             lcall fgets bp,si,91    ;Get string
  87.             test ax,ax              ;End of file?
  88.             loopz IndexLoop         ;Loop back
  89.  
  90.             mov dx,di               ;Restore DI, and put
  91.             pop di                  ;the line count in DX
  92.             sub dx,di
  93.             shr dx,2
  94.  
  95.             lcall inittext          ;Initialize text system
  96.             lcall setctype 2000h    ;Turn off cursor
  97.  
  98.             lcall setcolor 70h      ;Display help string
  99.             lcall tputs 0,33,offset(HelpStr)
  100.  
  101.             xor cx,cx               ;Current line = 0
  102.  
  103. MainLoop:   push dx                 ;Save DX
  104.             mov bx,cx               ;Index position
  105.             shl bx,2
  106.             push bp [di+bx+2]       ;Seek to line
  107.             push [di+bx] 0
  108.             lcall fseek
  109.             xor dx,dx               ;Zero DX
  110.  
  111. ReadLoop:   imul ax,dx,91           ;AX = pointer
  112.             add ax,si
  113.             lcall fgets bp,ax,91    ;Read line
  114.             inc dx                  ;Loop back
  115.             cmp dx,33
  116.             jl ReadLoop
  117.  
  118.             lcall setcolor 70h      ;Display current line...
  119.             inc cx                  ;Make it one-based
  120.             lcall itoa cx,offset(JunkBuf) ;Convert to string
  121.             dec cx
  122.             lcall hline 31,35,33,20h      ;Display the string
  123.             lcall tputs 31,33,offset(JunkBuf)
  124.  
  125.             lcall setcolor 07h      ;Set text color
  126.             xor dx,dx               ;Zero DX
  127. DispLoop:   lcall hline 0,89,dx,20h ;Clear the line
  128.             imul ax,dx,91           ;AX = pointer
  129.             add ax,si
  130.             lcall tputs 0,dx,ax     ;Display string
  131.             inc dx                  ;Loop back
  132.             cmp dx,33
  133.             jl DispLoop
  134.  
  135.             pop dx                  ;Restore DX
  136. KeyLoop:    xor ah,ah               ;Get a key
  137.             int 16h
  138.  
  139.             cmp ax,011Bh            ;Escape?
  140.             je Quit
  141.             cmp dx,33               ;File < 1 page,  no need
  142.             jle KeyLoop             ;to process other keys
  143.  
  144.             cmp ax,4800h            ;Up?
  145.             je KUp
  146.             cmp ax,5000h            ;Down?
  147.             je KDown
  148.             cmp ax,4900h            ;PgUp?
  149.             je KPgUp
  150.             cmp ax,5100h            ;PgDn?
  151.             je KPgDn
  152.             cmp ax,4700h            ;Home?
  153.             je KHome
  154.             cmp ax,4F00h            ;End?
  155.             je KEnd
  156.             jmp KeyLoop             ;Invalid, loop
  157.  
  158. KUp:        test cx,cx              ;Already at top?
  159.             jz KeyLoop
  160.             dec cx                  ;Go up one line
  161.             jmp MainLoop
  162.  
  163. KDown:      mov ax,cx               ;Already at bottom?
  164.             add ax,33
  165.             cmp ax,dx
  166.             jge KeyLoop
  167.             inc cx                  ;Go down one line
  168.             jmp MainLoop
  169.  
  170. KPgUp:      test cx,cx              ;Already at top?
  171.             jz KeyLoop
  172.             sub cx,33               ;Go up one page
  173.             jge MainLoop
  174.             xor cx,cx               ;Too far, go to top
  175.             jmp MainLoop
  176.  
  177. KPgDn:      mov ax,cx               ;Already at bottom?
  178.             add ax,33
  179.             cmp ax,dx
  180.             jge KeyLoop
  181.             mov cx,ax               ;Move down one page
  182.             add ax,33
  183.             cmp ax,dx
  184.             jle MainLoop
  185.             mov cx,dx               ;Too far, go to bottom
  186.             sub cx,33
  187.             jmp MainLoop
  188.  
  189. KHome:      xor cx,cx               ;Move to top
  190.             jmp MainLoop
  191.  
  192. KEnd:       mov cx,dx               ;Move to bottom
  193.             sub cx,33
  194.             jmp MainLoop
  195.  
  196. Quit:       lcall closetext         ;Clean up...
  197.             lcall freemem si
  198.             lcall freemem di
  199.             lcall fclose bp
  200.             ret                     ;Return
  201.  
  202. EndP        main
  203.  
  204. End Start
  205.